﻿using Magicodes.ExporterAndImporter.Core.Filters;
using Magicodes.ExporterAndImporter.Core.Models;
using RayD.Core;
using RayD.Repository;
using RayD.Repository.Entites.SystemModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RayD.App.Magicodes
{
    /// <summary>
    /// 导入结果筛选器，可以修改导入结果包括验证错误信息（比如动态修改错误标注）
    /// 在基础验证之后执行， 无法通过构造注入三方服务
    /// </summary>
    public class ImportUserFilter : IImportResultFilter
    {
        public ImportResult<T> Filter<T>(ImportResult<T> importResult) where T : class, new()
        {
            //获取导入数据源
            var sources = importResult.Data.ToList() as List<ImportUserDto>;
            if (sources.Count > 0)
            {
                var db = ServiceContainer.Current.GetInstance<IRepository>();

                #region 前期准备
                var userList = db.IQueryable(LinqExtensions.True<UserEntity>());
                var manaList = db.IQueryable(LinqExtensions.True<ManaframeEntity>());
                var depList = db.IQueryable(LinqExtensions.True<DepartmentEntity>()); 
                #endregion

                //遍历处理数据源
                foreach (var source in sources)
                {
                    //错误标注
                    var errorInfo = new DataRowErrorInfo()
                    {
                        RowIndex = sources.FindIndex(o => o.Equals(source)) + 2,
                    };

                    #region 参数验证
                    //账户
                    if (!source.B_Account.IsEmpty())
                    {
                        if (userList.Any(c => c.B_Account.Equals(source.B_Account)))
                        {
                            errorInfo.FieldErrors.Add("账户", "该账户已存在！");
                        }
                    }
                    //工号
                    if (!source.B_EnCode.IsEmpty())
                    {
                        if (userList.Any(c => c.B_EnCode.Equals(source.B_EnCode)))
                        {
                            errorInfo.FieldErrors.Add("工号", "该工号已存在！");
                        }
                    }
                    //所属机构
                    if (!source.B_ManaframeId.IsEmpty())
                    {
                        var manaEntity = manaList.FirstOrDefault(c => c.B_FullName.Equals(source.B_ManaframeId));
                        if (!manaEntity.IsEmpty())
                        {
                            source.B_ManaframeId = manaEntity.B_ManaframeId;//所属机构
                        }
                        else
                        {
                            errorInfo.FieldErrors.Add("所属机构", "该机构不存在！");
                        }
                    }
                    //所属部门
                    if (!source.B_DepartmentId.IsEmpty())
                    {
                        var depEntity = depList.FirstOrDefault(c => c.B_FullName.Equals(source.B_DepartmentId) && c.B_ManaframeId.Equals(source.B_ManaframeId));
                        if (!depEntity.IsEmpty())
                        {
                            source.B_DepartmentId = depEntity.B_DepartmentId;//所属部门
                        }
                        else
                        {
                            errorInfo.FieldErrors.Add("所属部门", "该部门不存在！");
                        }
                    }
                    #endregion

                    //添加错误标注
                    if (errorInfo.FieldErrors.Count > 0)
                    {
                        importResult.RowErrors.Add(errorInfo);
                    }
                }
                db.Commit();
            }
            return importResult;
        }

    }
}
